package cn.iocoder.yudao.module.bpm.controller.admin.taskinfo.vo;

import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.process.BpmProcessDefinitionRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.BpmProcessInstancePageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.instance.BpmProcessInstanceRespVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.*;
import java.util.*;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import com.alibaba.excel.annotation.*;

@Schema(description = "管理后台 - 流程任务信息 Response VO")
@Data
@ExcelIgnoreUnannotated
public class BpmTaskInfoRespVO {

    @Schema(description = "id", requiredMode = Schema.RequiredMode.REQUIRED, example = "29572")
    @ExcelProperty("id")
    private Long id;

    @Schema(description = "任务编号")
    @ExcelProperty("任务编号")
    private String code;

    @Schema(description = "任务名称", example = "赵六")
    @ExcelProperty("任务名称")
    private String name;

    @Schema(description = "任务类型：1-标准任务，2-流程任务", example = "1")
    @ExcelProperty("任务类型：1-标准任务，2-流程任务")
    private Integer type;

    @Schema(description = "表单类型", example = "1")
    @ExcelProperty("表单类型")
    private String formId;

    @Schema(description = "负责人id", example = "14824")
    @ExcelProperty("负责人id")
    private Long adminId;

    @Schema(description = "确认人id", example = "4669")
    @ExcelProperty("确认人id")
    private Long confirmId;

    @Schema(description = "关注人id", example = "31387")
    @ExcelProperty("关注人id")
    private Long attentionId;

    @Schema(description = "标签id", example = "11629")
    @ExcelProperty("标签id")
    private Long tagId;

    @Schema(description = "流程实例的编号", example = "25549")
    @ExcelProperty("流程实例的编号")
    private String processInstanceId;

    @Schema(description = "父任务", example = "15602")
    @ExcelProperty("父任务")
    private Long parentId;

    @Schema(description = "关联任务", example = "5575")
    @ExcelProperty("关联任务")
    private Long relationId;

    @Schema(description = "任务发起时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime taskStartTime;

    @Schema(description = "任务结束时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime taskEndTime;

    @Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
    @ExcelProperty("创建时间")
    private LocalDateTime createTime;

    @Schema(description = "任务描述", example = "随便")
    @ExcelProperty("任务描述")
    private String description;

    @Schema(description = "任务排序,order值大的排序靠前")
    @ExcelProperty("任务排序,order值大的排序靠前")
    private Long orderRank;

    @Schema(description = "项目id", example = "12")
    private Long projectId;

    @Schema(description = "项目名称", example = "测试项目")
    private String projectName;

    //##################   流程实例信息 #############################
    @Schema(description = "流程实例名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道")
    private String processDefinitionName;

    @Schema(description = "流程分类", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private String category;
    @Schema(description = "流程分类名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "请假")
    private String categoryName;

    @Schema(description = "流程实例的状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
    private Integer status; // 参见 BpmProcessInstanceStatusEnum 枚举 和任务状态同一个

    @Schema(description = "发起时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime startTime;

    @Schema(description = "结束时间", requiredMode = Schema.RequiredMode.REQUIRED)
    private LocalDateTime endTime;

    @Schema(description = "持续时间", example = "1000")
    private Long durationInMillis;

    @Schema(description = "提交的表单值", requiredMode = Schema.RequiredMode.REQUIRED)
    private Map<String, Object> formVariables;

    @Schema(description = "业务的唯一标识-例如说，请假申请的编号", example = "1")
    private String businessKey;

    /**
     * 发起流程的用户
     */
    private BpmProcessInstanceRespVO.User startUser;

    @Schema(description = "流程定义的编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
    private String processDefinitionId;
    /**
     * 流程定义
     */
    private BpmProcessDefinitionRespVO processDefinition;

    /**
     * 当前审批中的任务
     */
    private List<BpmProcessInstanceRespVO.Task> tasks; // 仅在流程实例分页才返回

    @Schema(description = "用户信息")
    @Data
    public static class User {

        @Schema(description = "用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
        private Long id;
        @Schema(description = "用户昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
        private String nickname;

        @Schema(description = "部门编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
        private Long deptId;
        @Schema(description = "部门名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "研发部")
        private String deptName;

    }

    @Schema(description = "流程任务")
    @Data
    public static class Task {

        @Schema(description = "流程任务的编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
        private String id;

        @Schema(description = "任务名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋道")
        private String name;

    }


    public static BpmTaskInfoRespVO builder(BpmTaskInfoRespVO target,BpmProcessInstanceRespVO source){
        target.setCategory(source.getCategory());
        target.setTasks(source.getTasks());
        target.setProcessDefinition(source.getProcessDefinition());
        target.setStartUser(source.getStartUser());
        //流程定义名称
        target.setProcessDefinitionName(source.getName());
        target.setCategory(source.getCategory());
        target.setCategoryName(source.getCategoryName());
        target.setStatus(source.getStatus());
        target.setStartTime(source.getStartTime());
        target.setEndTime(source.getEndTime());
        target.setDurationInMillis(source.getDurationInMillis());
        target.setFormVariables(source.getFormVariables());
        target.setBusinessKey(source.getBusinessKey());
        target.setProcessDefinitionId(source.getProcessDefinitionId());
        return target;
    }

}